'''
Program Assignment:	Assignment 2
Name:			Alberto Massa                                                                      
Date:			11/08/2010                  
Description:            This file contains custom widget 
'''

'''
Listing Contents:
Compilation instructions
	This module does not contains runnable standalone code.
	Theese widget are designed for funcounter object, that module must be on PYTHONPATH.
	or must be loaded in runtime exection scope.

Includes and dependecies
	PyQt4 are needed.

Class and procedures declarations:
	TableWidget: Custom QTableWidget
	TreeWidget: Custom QTreeWidget

'''

from PyQt4.QtGui import	QTableWidget, QTableWidgetItem
from PyQt4.QtGui import	QTreeWidget, QTreeWidgetItem

class TableWidget(QTableWidget):
	'''
	Class for handle visualization of CodeFile objects inside a QTableWidget.
	Three colums inside the table for name of file, path, size in byte.
	'''

	def __init__(self, container):
		'''
		Constructor that take in input container.
		Automathically this widget will incorporate the geometry rect property of container.
		Usage:
			Somewhere (QtDesigner or via code) construct a container, that could be every subclass of QWidget.
			Set dimension with rect property.
			Call QTableWidget(container).
		'''
		QTableWidget.__init__(self, container)

		self.setGeometry(container.rect())
		self.setColumnCount(3)
		self.setHorizontalHeaderLabels(["File","Path","Size in byte"])
		self.setColumnWidth(0, 205)
		self.setColumnWidth(1, 355)

		self.path2codefile = dict()

	def clear(self):
		'''
		Clear all items.
		'''
		QTableWidget.clear(self)
		self.path2codefile = dict()


	def populate(self, files):
		'''
		Populate table.
		Take in input a list of CodeFile object.
		'''
		files = list(files)

		self.clear()
		self.setRowCount(len(files))
		for i,_file in enumerate(files):
			self.add_filetable_item(_file, i)

	def add_filetable_item(self, _file, i):
		'''
		Add a single item made up on a CodeFile object, 
		in position i.
		'''
		self.path2codefile[_file.path] = _file

		name = QTableWidgetItem(_file.name)
		self.setItem(i, 0, name)

		path = QTableWidgetItem(_file.path)
		self.setItem(i, 1, path)

		size = QTableWidgetItem(str(_file.size_in_byte))
		self.setItem(i, 2, size)


class TreeWidget(QTreeWidget):
	'''
	Class that handle visualization of a Token object.
	Each item visualized has also a column with total line of code of block represented by Token object.
	'''

	def __init__(self, container):
		'''
		Constructor that take in input container.
		Automathically this widget will incorporate the geometry rect property of container.
		Usage:
			Somewhere (QtDesigner or via code) construct a container, that could be every subclass of QWidget.
			Set dimension with rect property.
			Call QTreeWidget(container).
		'''
		QTreeWidget.__init__(self, container)
		
		self.setGeometry(container.rect())		
		self.setItemsExpandable(True)
		self.setHeaderLabels(["Token name", "Loc", "Size"])

		self.token2item = dict()

	def clear(self):
		'''
		Clear all items
		'''
		QTreeWidget.clear(self)
		self.token2item = dict()

	def visit(self, root):
		'''
		Take Token object and populate tree widget.
		'''
		def _add_tree_item(token):
			try:
				# pick parent from dictionary
				item_parent = self.token2item[token.parent]
				# create treewidgetitem
				item = QTreeWidgetItem(item_parent, [token.name])
				
				#i = TableWidgetItem("asd")
				#item.add(i)
			except:
				# firstofall, creare root item
				item = QTreeWidgetItem(self, ["root"])

			item.setData(1, 0, str(token.line))
			item.setData(2, 0, str(len(token)))
			self.token2item[token] = item
			# recurively go to all childs
			childs = token.childs
			for c in childs:
				_add_tree_item(c)

		self.clear()
		_add_tree_item(root)



